home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9207.ZIP / AVKCAPT.ZIP / DISPERR.C < prev    next >
C/C++ Source or Header  |  1992-01-27  |  11KB  |  393 lines

  1. //-------------------------------------------------------------------------
  2. //            ActionMedia II Programmer's Toolkit
  3. //            
  4. //            Windows Sample Code Shared Functions
  5. //
  6. // Module Name:    DispErr
  7. //
  8. // Description:    This module contains functions for displaying messages and
  9. //                errors in Windows Message Boxes.
  10. //
  11. // Copyright:    Copyright (c) Intel Corporation, 1992
  12. //
  13. //-------------------------------------------------------------------------
  14. //
  15. // Exported functions from this module:
  16. //
  17. //        InitErrSystem
  18. //        CloseErrSystem
  19. //        DispMsg
  20. //        DispErr
  21. //        DispAvkErr
  22. //        TimeString
  23. //        DateString
  24. //
  25. //-------------------------------------------------------------------------
  26. // NOTES on DispErr
  27. //    
  28. //    These functions are used in most of the sample applications to 
  29. //    display messages, generic errors and AVK errors in Windows Message 
  30. //    Boxes.
  31. //
  32. //    They can be made to log error messages to a log file by #defining
  33. //    the manifest constant LOGGING.  Messages will be logged provided
  34. //    that the application has previously called LogOpen().
  35. //    
  36. //    Also included are two functions, TimeString() and DateString(), 
  37. //    which get the system time and data and format them into strings.
  38. //    
  39. //-------------------------------------------------------------------------
  40. #include <windows.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <dos.h>
  44. #include "avkapi.h"
  45. #include "bitflags.h"
  46. #include "log.h"
  47.  
  48. //     Undocumented AVK function to return an error description string for
  49. //    a given AVK error value.  This is not supported and should not be 
  50. //    used in production code.
  51.  
  52. extern AVK_CALL AvkGetErrString(U16 ErrCode, I16 Len, char far *pDestin);
  53.  
  54. #define    DE_INIT            0x01
  55. #define    DE_LOG_ON        0x02
  56. #define DE_INCL_DATE    0x04
  57. #define    DE_INCL_TIME    0x08
  58.  
  59. static unsigned short    DeStatus = 0;
  60.  
  61. static HAVK     hAvk = (HAVK)0;
  62.  
  63. char       *TimeString(VOID);
  64. char       *DateString(VOID);
  65.  
  66.  
  67. //-------------------------------------------------------------------------
  68. //FUNCTION:
  69. //
  70. //    void InitErrSystem(hAvkSession, bLogging, bShowDate, bShowTime)
  71. //    
  72. //PARMS IN:
  73. //    
  74. //    HAVK    hAvkSession        AVK session handle returned from AvkBeginMsg().
  75. //    BOOL    bLogging        If TRUE then logging is desired
  76. //    BOOL    bShowDate        If TRUE then logging includes a date-stamp
  77. //    BOOL    bShowTime        If TRUE then logging includes a time-stamped
  78. //    
  79. //DESCRIPTION:
  80. //
  81. //    Initialize message display function.  This allows the application to
  82. //    register the AVK session handle for use when calling AvkGetErrString()
  83. //    in DispAvkErr().  Boolean flags can also be set to turn logging on
  84. //    and select date- and time-stamping in the log file.
  85. //
  86. //    NO MESSAGE BOXES WILL BE DISPLAYED UNTIL THIS FUNCTION HAS BEEN CALLED
  87. //    TO INITIALIZE THE SYSTEM.
  88. //
  89. //    To turn on logging, the application needs to pass the AVK session
  90. //    handle retrieved from AvkBeginMsg() or AvkBeginCallback().  Either
  91. //    of these functions must be called before InitErrSystem() to obtain
  92. //    the handle.  If no logging is desired, the InitErrSystem can be
  93. //    called before the AVK session is begon.
  94. //
  95. //    Note that InitErrSystem can be called more than once in an application
  96. //    to change the logging status.  Thus, an application can call 
  97. //    InitErrSystem() with a null AVK handle and FALSE for the bLogging
  98. //    parameter at the beginning of the application to enable error reporting
  99. //    and then call it again after AvkBeginMsg() to enable logging, and
  100. //    again periodically to turn logging off and on.
  101. //
  102. //    InitErrSystem() can also be used in conjunction with CloseErrSystem()
  103. //    to turn error reporting on and off in an application.
  104. //
  105. //-------------------------------------------------------------------------
  106.  
  107. void
  108. InitErrSystem(HAVK hAvkSession, BOOL bLogging, BOOL bShowDate, BOOL bShowTime)
  109. {
  110.     //    If a null AVK session handle has
  111.     if (hAvkSession == (HAVK)0)
  112.         bLogging = FALSE;
  113.     else
  114.         hAvk = hAvkSession;
  115.  
  116.     if (bLogging)
  117.     {
  118.         SetFlag(DeStatus, DE_LOG_ON);
  119.     
  120.         if (bShowDate)
  121.             SetFlag(DeStatus, DE_INCL_DATE);
  122.         if (bShowTime)
  123.              SetFlag(DeStatus, DE_INCL_TIME);
  124.     }
  125.  
  126.     SetFlag(DeStatus, DE_INIT);
  127. }
  128.  
  129. //-------------------------------------------------------------------------
  130. //FUNCTION:
  131. //
  132. //    void CloseErrSystem(VOID)
  133. //    
  134. //DESCRIPTION:
  135. //
  136. //    Turns off error reporting.  The error call functions will still
  137. //    return as normal, but will not display any Message Box or log
  138. //    any errors to the log file.
  139. //
  140. //-------------------------------------------------------------------------
  141.  
  142. void
  143. CloseErrSystem()
  144. {
  145.     ClearFlag(DeStatus, DE_INIT);
  146. }
  147.  
  148. //-------------------------------------------------------------------------
  149. //FUNCTION:
  150. //
  151. //    BOOL DispMsg(pMsg)
  152. //
  153. //PARMS IN:
  154. //
  155. //    char *pMsg;            pointer to message to be displayed
  156. //    
  157. //DESCRIPTION:
  158. //    
  159. //    Display a message in a message box and wait for the user to press
  160. //    the OK button.
  161. //    
  162. //RETURN:
  163. //    Always returns TRUE.
  164. //
  165. //-------------------------------------------------------------------------
  166.  
  167. BOOL
  168. DispMsg(char *pMsg)
  169. {
  170.     if (TestFlag(DeStatus, DE_INIT))
  171.     {
  172.         //    Display the message in a window message box and return 'ok'.
  173.  
  174.         MessageBox(NULL, pMsg, "Application Message", 
  175.           MB_ICONINFORMATION | MB_OK);
  176.  
  177.         if (TestFlag(DeStatus, DE_LOG_ON))
  178.         {
  179.             if (TestFlag(DeStatus, DE_INCL_DATE))
  180.                 LogWrite("%s ", DateString());
  181.             if (TestFlag(DeStatus, DE_INCL_TIME))
  182.                 LogWrite("%s ", TimeString());
  183.             LogWrite("%s"CR, pMsg);
  184.         }
  185.     }
  186.     return TRUE;
  187. }
  188.  
  189.  
  190. //-------------------------------------------------------------------------
  191. //FUNCTION:
  192. //
  193. //    BOOL DispErr(pLoc, pErr)
  194. //
  195. //PARMS IN:
  196. //
  197. //    char *pLog;            pointer to a string with the name of the function
  198. //                            that produced the error.
  199. //    char *pErr;            pointer to an error message to be displayed.
  200. //    
  201. //DESCRIPTION:
  202. //    
  203. //    Display a generic error message in a Window's Message Box and wait 
  204. //    for the user to press the OK button. This function always returns
  205. //    the Boolean error value, FALSE, so an application can include a
  206. //    call to DispErr() within a return call, as follows:
  207. //
  208. //        if (MyFunc() == FALSE)
  209. //            return DispErr("MyFunc", "My function failed");
  210. //    
  211. //RETURN:
  212. //
  213. // Always returns FALSE for error.
  214.  
  215.  
  216.  
  217. //    Always returns TRUE.
  218. //
  219. //-------------------------------------------------------------------------
  220.  
  221. BOOL
  222. DispErr(char *pLoc, char *pErr)
  223. {
  224.     char Buf[128];
  225.  
  226.     if (TestFlag(DeStatus, DE_INIT))
  227.     {
  228.  
  229.         //    Make up a message and display it in a window message box
  230.  
  231.         sprintf(Buf, "ERROR: %s", pErr);
  232.         MessageBox(NULL, Buf, pLoc, MB_OK | MB_ICONEXCLAMATION);
  233.  
  234.         //    Log it and return an error value.
  235.  
  236.         if (TestFlag(DeStatus, DE_LOG_ON))
  237.         {
  238.             if (TestFlag(DeStatus, DE_INCL_DATE))
  239.                 LogWrite("%s ", DateString());
  240.             if (TestFlag(DeStatus, DE_INCL_TIME))
  241.                 LogWrite("%s ", TimeString());
  242.             LogWrite("%s: %s"CR, pLoc, Buf);
  243.         }
  244.     }
  245.     return FALSE;
  246. }
  247.  
  248.  
  249. //-------------------------------------------------------------------------
  250. //FUNCTION:
  251. //
  252. //    BOOL DispAvkErr(AvkRet, pFunc)
  253. //
  254. //PARMS IN:
  255. //
  256. //    int        AvkRet        The error value returned from a failed AVK API
  257. //                            function.
  258. //    char     *pFunc        Pointer to string with the failed function's name.
  259. //    
  260. //DESCRIPTION:
  261. //    
  262. //    Display an AVK error message in a Window's Message Box and wait 
  263. //    for the user to press the OK button.  This function uses an 
  264. //    undocumented AVK API call, AvkGetErrString() to retrieve a string
  265. //    from AVK describing the error.  This call should not be used in
  266. //    production code, since, as an unsupported function, it is not
  267. //    guaranteed to return the correct error description in future
  268. //    version (if it exists at all).  DispAvkErr() also extracts the
  269. //    AVK subsystem error, if any.  If an AVK API function fails in
  270. //    a lower-level 'subsystem', a subsystem error will give additional
  271. //    information on the exact location and nature of the failure.
  272. //    Like DispErr(), this function always returns the Boolean error value, 
  273. //    FALSE, so an application can include a call to DispAvkErr() within a 
  274. //    return call, as follows:
  275. //
  276. //    if ((AvkRet = AvkBeginMsg(hWnd, &hAvk, AVK_SESSION_DEFAULT) != AVK_ERR_OK)
  277. //        return DispAvkErr(AvkRet, "AvkBeginMsg");
  278. //    
  279. //RETURN:
  280. //
  281. // Always returns FALSE for error.
  282. //
  283. //-------------------------------------------------------------------------
  284.  
  285. BOOL
  286. DispAvkErr(int AvkRet, char *pFunc)
  287. {
  288.     char     Buf1[128];
  289.     char    Buf2[128];
  290.     U16        SubSysErr = 0;
  291.  
  292.     if (TestFlag(DeStatus, DE_INIT))
  293.     {
  294.  
  295.         //    Get error string and subsystem error from AVK.  If we don't
  296.         //    have access to the AVK session handle, set the subsystem
  297.         //    error to 0xffff.
  298.  
  299.         if (AvkGetErrString(AvkRet, 80, (char *)Buf1))
  300.              sprintf(Buf1, "AVK ERROR %x", AvkRet);
  301.  
  302.         if (hAvk != (HAVK)0)
  303.             AvkGetSubSysErrCode(hAvk, &SubSysErr);
  304.         else
  305.             SubSysErr = 0xffff;
  306.  
  307.         //    Make up a message and display it in a window message box
  308.  
  309.         sprintf(Buf2, "%s [SubSys 0x%04x]", Buf1, SubSysErr);
  310.  
  311.         MessageBox(GetFocus(), Buf2, pFunc, MB_OK | MB_ICONHAND);
  312.  
  313.         //    Log it and return the error value.
  314.  
  315.         if (TestFlag(DeStatus, DE_LOG_ON))
  316.         {
  317.             if (TestFlag(DeStatus, DE_INCL_DATE))
  318.                 LogWrite("%s ", DateString());
  319.             if (TestFlag(DeStatus, DE_INCL_TIME))
  320.                 LogWrite("%s ", TimeString());
  321.  
  322.             LogWrite("%s - %s"CR, pFunc, Buf2);
  323.         }
  324.     }
  325.     return FALSE;
  326. }
  327.  
  328. //-------------------------------------------------------------------------
  329. //FUNCTION:
  330. //    
  331. //    char *TimeString(VOID)
  332. //
  333. //DESCRIPTION:
  334. //    
  335. //    Extracts the computer's system time and builds a string in the format:
  336. //    
  337. //        hh:mm:ss.xx 
  338. //    
  339. //    where hh=hours, mm=minutes, ss=seconds and xx=hundredths of a second.
  340. //
  341. //
  342. //RETURN:
  343. //
  344. //    Pointer to an internal buffer which holds the formatted time string.
  345. //
  346. //-------------------------------------------------------------------------
  347.     
  348. char *
  349. TimeString(VOID)
  350. {
  351.     static char         Buf[16];
  352.     struct dostime_t    Tm;
  353.  
  354.     _dos_gettime(&Tm);
  355.     sprintf(Buf, "%02u:%02u:%02u.%02u", Tm.hour, Tm.minute, Tm.second, 
  356.       Tm.hsecond);
  357.     return Buf;
  358. }
  359.  
  360. //-------------------------------------------------------------------------
  361. //FUNCTION:
  362. //    
  363. //    char *DateString(VOID)
  364. //
  365. //DESCRIPTION:
  366. //    
  367. //    Extracts the computer's system date and builds a string in the format:
  368. //    
  369. //        mm/dd/yy 
  370. //    
  371. //    where mm=month, dd=day and yy=year.
  372. //
  373. //
  374. //RETURN:
  375. //
  376. //    Pointer to an internal buffer which holds the formatted date string.
  377. //
  378. //-------------------------------------------------------------------------
  379.     
  380. char *
  381. DateString(VOID)
  382. {
  383.     static char Buf[16];
  384.     struct dosdate_t    Dt;
  385.  
  386.     _dos_getdate(&Dt);
  387.     sprintf(Buf, "% 2u/%02u/%02u", Dt.month, Dt.day, Dt.year % 100);
  388.     return Buf;
  389. }
  390.  
  391.  
  392.  
  393.